package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import com.jt.vo.itemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Override
    public PageResult getItemList(PageResult pageResult) {
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag, "title", pageResult.getQuery());

        //编辑MP的分页对象 四个属性有用(页数/条数/总数/记录)  传递=页数/条数
        IPage<Item> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        page = itemMapper.selectPage(page, queryWrapper);
        //获取总数
        long total = page.getTotal();
        //获取记录数
        List<Item> rows = page.getRecords();
        //将数据封装
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    public void saveItem(itemVO itemVO) {
        Item item = itemVO.getItem(); //id=null
        item.setStatus(true);
        //要求item入库之后,动态返回Id!!!!
        //MP原则: 入库之后动态回显数据!!
        itemMapper.insert(item);
        //实现itemDesc对象入库
        ItemDesc itemDesc = itemVO.getItemDesc();
        itemDesc.setId(item.getId());
        itemDescMapper.insert(itemDesc);
    }


    @Override
    public void deleteItemById(Integer id) {
        itemMapper.del(id);
        itemDescMapper.dl(id);
    }

    @Override
    public void updateItemStatus(Item item) {
       Item item1 =  item.setId(item.getId()).setStatus(item.getStatus());
        itemMapper.updateItemStatus(item1);
    }

//    @Override
//    public SysResult findAll(Integer id) {
//
//        return itemMapper.findAll(id);
//    }
}
